package com.yongcheon.sms_manager.data.phone_spsm;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.CallLog;
import android.widget.CursorAdapter;

import com.yongcheon.sms_manager.ui.phone_spam.list.PhoneCursorAdapter;
import com.yongcheon.sms_manager.vo.PhoneVO;

import java.util.ArrayList;

/**
 * Created by yongcheon on 2014-12-23.
 */
public class PhoneData extends AbstractPhoneData {

    @Override
    public CursorAdapter getCurosrAdapter(Context context, SQLiteDatabase database) {
        Cursor c = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, "type <> ?", new String[]{"2"}, "date desc");
//        Cursor c = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, "date desc");
        CursorAdapter adapter = new PhoneCursorAdapter(context, c);
        return adapter;
    }

    @Override
    public void insert(PhoneVO vo, Context context, SQLiteDatabase database) {
        ContentValues cv = new ContentValues();
        cv.put("number", vo.getNumber());
        cv.put("voicemail_uri", vo.getVoicemail_uri());
//        cv.put("features", vo.getFeatures());
        cv.put("new", vo.get_new());
        cv.put("formatted_number", vo.getFormatted_number());
        cv.put("numbertype", vo.getNumbertype());
        cv.put("date", vo.getDate());
        cv.put("duration", vo.getDuration());
//        cv.put("data_usage", vo.getData_usage());
//        cv.put("presentation", vo.getPresentation());
        cv.put("numberlabel", vo.getNumberlabel());
        cv.put("geocoded_location", vo.getGeocoded_location());
        cv.put("lookup_uri", vo.getLookup_uri());
        cv.put("countryiso", vo.getCountryiso());
//        cv.put("transcription", vo.getTranscription());
//        cv.put("subscription_component_name", vo.getSubscription_component_name());
        cv.put("name", vo.getName());
        cv.put("matched_number", vo.getMatched_number());
//        cv.put("subscription_id", vo.getSubscription_id());
        cv.put("photo_id", vo.getPhoto_id());
        cv.put("normalized_number", vo.getNormalized_number());
        cv.put("type", vo.getType());
        cv.put("is_read", vo.getIs_read());
        context.getContentResolver().insert(CallLog.Calls.CONTENT_URI, cv);
    }

    @Override
    public void delete(long id, Context context, SQLiteDatabase database) {
        String _id = String.valueOf(id);
        int result = context.getContentResolver().delete(CallLog.Calls.CONTENT_URI, "_id=?", new String[]{_id});
    }

    @Override
    public void deleteByPhoneNum(String num, Context context, SQLiteDatabase database) {
        int result = context.getContentResolver().delete(CallLog.Calls.CONTENT_URI, "number=?", new String[]{num});
    }

    public ArrayList<PhoneVO> select(String number, Context context, SQLiteDatabase database){
        Cursor c = null;
        if(number != null){
            c = context.getContentResolver().query(CallLog.Calls.CONTENT_URI,null,  "number=?", new String[]{number}, "date desc");
        }else{
            c = context.getContentResolver().query(CallLog.Calls.CONTENT_URI,null,  null, null, "date desc");
        }
        return getPhoneListByNum(c);
    }



}
